package com.mobilesales.android.model.dictionary;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Dictionary<K,V> extends ArrayList<KeyValuePair<K,V>>{
	public V getValue(K key){
		Iterator<KeyValuePair<K, V>> iter = super.iterator();
		V value = null;
		while(iter.hasNext()){
			 KeyValuePair<K, V> actualPair = iter.next();
			 if(actualPair.getKey().equals(key)){
				 value = actualPair.getValue();
				 break;
			 }
		}
		return value;
	}
	
	public V removeValue(K key){
		Iterator<KeyValuePair<K, V>> iter = super.iterator();
		V value = null;
		while(iter.hasNext()){
			 KeyValuePair<K, V> actualPair = iter.next();
			 if(actualPair.getKey().equals(key)){
				 value = actualPair.getValue();
				 iter.remove();
				 break;
			 }
		}
		return value;
	}
	
	public void addValue(K key, V value){
		Iterator<KeyValuePair<K, V>> iter = super.iterator();
		while(iter.hasNext()){
			 KeyValuePair<K, V> actualPair = iter.next();
			 if(actualPair.getKey().equals(key)){
				 actualPair.setValue(value);
				 return;
			 }
		}
		KeyValuePair<K, V> newKVPair = new KeyValuePair<K, V>();
		newKVPair.setKey(key);
		newKVPair.setValue(value);
		super.add(newKVPair);
	}
	
	public List<K> getKeys() {
		ArrayList<K> keys = new ArrayList<K>();
		for(int i=0; i<this.size(); i++) {
			keys.add(this.get(i).getKey());
		}
		return keys;
	}	
	
	public Boolean containsKey(K key){
		Iterator<KeyValuePair<K, V>> iter = super.iterator();
		while(iter.hasNext()){
			 KeyValuePair<K, V> actualPair = iter.next();
			 if(actualPair.getKey().equals(key)){
				 return true;
			 }
		}
		return false;
	}
}
